<template>
  <div>
    <el-row style="margin: 80px 0 0 0;">
      <el-col :span="12">&nbsp;</el-col>
      <el-col :span="9">
        <h1 style="text-align: center; margin: 20px 0;">用户登录</h1>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" size="small"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" size="small"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button size="small" @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <div style="text-align: center;">
          <el-link href="/admin">直接进入后面管理界面（临时）</el-link>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          let url = 'http://localhost:9081/admins/login';
          console.log('url >>> ' + url);
          let data = {
            'username': this.ruleForm.username,
            'password': this.ruleForm.password
          };
          console.log('data >>> ');
          console.log(data);
          this.axios.post(url, data).then((response) => {
            console.log(response.data.code);
            if (response.data.code == 20000) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              let jwt = response.data.data;
              console.log(jwt);
              localStorage.setItem('jwt',jwt);
            }  else {
              this.$message.error('登录失败，用户名或密码错误！');
            }
          }).catch(function(error) {
            console.log('响应结果为失败！');
          })
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>